package com.example.testvolication.controller;

import com.example.testvolication.pojo.dto.*;
import com.example.testvolication.pojo.result.R;
import com.example.testvolication.pojo.valid.group.NameAndTypeGroup;
import com.example.testvolication.pojo.valid.group.NameGroup;
import com.example.testvolication.pojo.valid.group.TypeGroup;
import com.example.testvolication.service.TestGroupService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author zhangs
 * @date 2022/5/18 10:29
 */
@RestController
@RequestMapping("/")
public class DemoController {

    private static R RESULT = R.ok("OK");

    private static R FAIL = R.fail(400 , "默认出错");

    @Resource
    public TestGroupService testGroupService;

    /**
     * 服务连通
     *
     * @return
     */
    @GetMapping("testOK")
    public R testOK() {
        return RESULT;
    }

    /**
     * 测试volicate注解
     *
     * @param demo
     * @return
     */
    @PostMapping("testValidParam")
    public R testValidParam(@RequestBody @Valid Demo demo) {
        return RESULT;
    }

    /**
     * <P>测试级联校验</P>
     * @param cascadeDemo
     * @return
     */
    @PostMapping("testValidCasCodeParam")
    public R testValidCasCodeParam(@RequestBody @Validated CascadeDemo cascadeDemo){
        return RESULT;
    }

    /**
     * <p>测试group生效,只针对姓名</p>
     * @param groupingDemo
     * @return
     */
    @PostMapping("testGrouping1")
    public R testGrouping1(@RequestBody @Validated(NameGroup.class)GroupingDemo groupingDemo){
        return RESULT;
    }

    /**
     * <p>测试group生效,只针对type</p>
     * @param groupingDemo
     * @return
     */
    @PostMapping("testGrouping2")
    public R testGrouping2(@RequestBody @Validated(TypeGroup.class)GroupingDemo groupingDemo){
        return RESULT;
    }

    /**
     * <p>
     *     测试group生效,针对name和type
     * </p>
     * @param groupingDemo
     * @return
     */
    @PostMapping("testGrouping3")
    public R testGrouping3(@RequestBody @Validated(NameAndTypeGroup.class)GroupingDemo groupingDemo){
        return RESULT;
    }

    /**
     * <p>测试分组在service层的生效情况</p>
     * <p>
     *     结论: 不在controller处使用组不生效
     * </p>
     * @param groupingDemo
     * @return
     */
    @PostMapping("testGrouping")
    public R testGrouping(@RequestBody @Validated GroupingDemo groupingDemo){
        R result = FAIL;
        Integer cased = groupingDemo.getIsCase();
        if (null == cased){
            return result;
        }
        switch (cased){
            case 1:
                result = testGroupService.testGrouping1( groupingDemo);
                break;
            case 2:
                result = testGroupService.testGrouping2(groupingDemo);
                break;
            case 3:
                result = testGroupService.testGrouping3(groupingDemo);
                break;
            default:
                break;
        }
        return result;
    }

    /**
     * <p>测试自定义注解加字段校验</p>
     * @param annotationDemo
     * @return
     */
    @PostMapping("testAnnotationInField")
    public R testAnnotationInField(@RequestBody @Validated AnnotationDemo annotationDemo){
        return RESULT;
    }

    /**
     * <p>
     *     测试自定义注解加全类字段校验策略
     * </p>
     * @param annotationClassDemo
     * @return
     */
    @PostMapping("testAnnotationInClassAllField")
    public R testAnnotationInClassAllField(@RequestBody @Validated AnnotationClassDemo annotationClassDemo){
        return RESULT;
    }


}
